<script setup lang="ts">
import {
  deleteMemberAddressByIdAPI,
  getMemberAddressAPI,
} from '@/services/address';
import { useAddressStore } from '@/stores/modules/address';
import type { AddressItem } from '@/types/address';
import { onShow } from '@dcloudio/uni-app';
import { ref } from 'vue';

//获取收货地址列表
const addressList = ref<AddressItem[]>([]);
const getMemberAddressData = async () => {
  uni.showLoading();
  const res = await getMemberAddressAPI();
  uni.hideLoading();
  addressList.value = res.result;
};
// 初始化调用（页面显示）
onShow(() => {
  getMemberAddressData();
});

// 删除收获地址
const onDeleteAddress = (id: string) => {
  uni.showModal({
    content: '确认删除该收货地址吗？',
    success: async (res) => {
      if (res.confirm) {
        // 删除地址
        await deleteMemberAddressByIdAPI(id);
        // 重新加载收获地址列表
        getMemberAddressData();
      }
    },
  });
};

// 选择收货地址
const onSelectAddress = (item: AddressItem) => {
  // 修改选中的收货地址
  const addressStore = useAddressStore();
  addressStore.changeSelectAddress(item);
  // 返回上一页
  uni.navigateBack();
};
</script>

<template>
  <view class="viewport">
    <!-- 地址列表 -->
    <scroll-view class="scroll-view" scroll-y>
      <view v-if="addressList.length" class="address">
        <uni-swipe-action class="address-list">
          <!-- 收货地址项 -->
          <uni-swipe-action-item
            class="item"
            v-for="item in addressList"
            :key="item.id"
          >
            <view class="item-content" @tap="onSelectAddress(item)">
              <view hover-class="none" class="avatar">{{
                item.receiver.length > 2 ? item.receiver[0] : item.receiver
              }}</view>
              <view class="user">
                {{ item.receiver }}
                <text class="contact">{{ item.contact }}</text>
                <text v-if="item.isDefault" class="badge">默认</text>
              </view>
              <view class="locate"
                >{{ item.fullLocation }} {{ item.address }}</view
              >
              <navigator
                class="edit"
                hover-class="none"
                :url="`/pagesMember/address-form/address-form?id=${item.id}`"
                @tap.stop="() => {}"
              >
                <uni-icons
                  class="icons"
                  type="compose"
                  color="#c7c7c7"
                  size="20"
                />
              </navigator>
            </view>
            <template #right>
              <button class="delete-button" @tap="onDeleteAddress(item.id)">
                <uni-icons class="icons" type="trash" color="#fff" size="20" />
                <view class="delete-text">删除</view>
              </button>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
      <view v-else class="blank">暂无收货地址</view>
    </scroll-view>
    <!-- 添加按钮 -->
    <view class="add-container">
      <view class="add-btn">
        <navigator
          hover-class="none"
          url="/pagesMember/address-form/address-form"
        >
          新建地址
        </navigator>
      </view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

::-webkit-scrollbar {
  width: 0;
  height: 0;
  display: none;
}

/* 删除按钮 */
.delete-button {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 100%;
  font-size: 28rpx;
  color: #fff;
  border-radius: 0;
  padding: 0;
  background-color: #e43d33;

  .icons {
    margin-bottom: -41rpx;
  }

  .delete-text {
    margin-bottom: 17rpx;
  }
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f4f4f4;
  overflow: scroll;

  .scroll-view {
    flex: 1;
    padding-top: 20rpx;
    margin-bottom: 150rpx;
  }
}

.address {
  padding-left: 20rpx;
  margin: 0 20rpx;
  border-radius: 20rpx;
  background-color: #fff;

  .item-content {
    line-height: 1;
    padding: 30rpx 10rpx 30rpx;
    border-bottom: 1rpx solid #ddd;
    position: relative;

    .avatar {
      float: left;
      width: 85rpx;
      height: 85rpx;
      background-color: rgba($color: papayawhip, $alpha: 0.6);
      border-radius: 50%;
      text-align: center;
      line-height: 85rpx;
      color: #ec602b;
      margin-right: 10px;
      font-size: 15px;
    }

    .edit {
      position: absolute;
      top: 36rpx;
      right: 50rpx;
      padding: 2rpx 0 2rpx 20rpx;
      // border-left: 1rpx solid #666;
      font-size: 26rpx;
      color: #666;
      line-height: 1;

      .icons {
        position: absolute;
        top: 25rpx;
        right: -30rpx;
      }
    }
  }

  .item:last-child .item-content {
    border: none;
  }

  .user {
    font-size: 28rpx;
    margin-bottom: 20rpx;
    color: #333;

    .contact {
      color: #666;
    }

    .badge {
      display: inline-block;
      padding: 6rpx 8rpx 6rpx 8rpx;
      margin: 2rpx 0 0 10rpx;
      font-size: 23rpx;
      color: #ec602b;
      border-radius: 6rpx;
      border: 1rpx solid #ec602b;
    }
  }

  .locate {
    display: flex;
    flex-wrap: wrap;
    line-height: 1.6;
    font-size: 26rpx;
    color: #333;
    width: 75%;
  }
}

.blank {
  margin-top: 300rpx;
  text-align: center;
  font-size: 32rpx;
  color: #c7c9ce;
}

.add-container {
  width: 100%;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  padding-bottom: 35rpx;

  .add-btn {
    width: 95%;
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 10rpx 20rpx 10rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #ec602b;
  }
}
</style>
